'''
s7界面调试主程序
'''

import sys
import os
from PyQt5 import QtWidgets
from s7_client_ui import Ui_MainWindow  # 界面控件， QtDesigner生成

sys.path.append('D:\code\git\zxc\huoni\project_huoni\communication\plc_S7')
from s7_client import S7Client


class MyQMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyQMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.s7cl = S7Client()
        self.data = '0'

    def connect_slot(self):
        # QMessageBox.information(self, '信息', '这是一个PyQt5应用')

        tex = self.pushButton.text()
        if tex == "连接":
            self.s7cl.connect()  # 槽函数捕获异常 依然会退出程序？
        elif tex == "关闭连接":
            self.s7cl.close()

        if self.s7cl.get_connected():
            self.label.setText("已连接")
            self.pushButton.setText("关闭连接")
        else:
            self.label.setText("未连接")
            self.pushButton.setText("连接")

    def write_slot(self):
        # QMessageBox.information(self, '信息', '这是一个PyQt5应用')
        if self.s7cl.get_connected():
            data_bytes = bytes(self.data, encoding='UTF-8')
            self.s7cl.write(data_bytes)

    def read_slot(self):
        if self.s7cl.get_connected():
            out_data_bytearray = self.s7cl.read()
            out_data_str = str(out_data_bytearray, 'UTF-8')
            self.label_2.setText(out_data_str)

    def load_data_slot(self):
        # QMessageBox.information(self, '信息', '这是一个PyQt5应用')
        self.data = self.lineEdit.text()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWnd = MyQMainWindow()
    # ui = Ui_MainWindow()
    # ui.setupUi(mainWnd)
    mainWnd.show()
    sys.exit(app.exec())
